/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gzwanchuang.xhgj.ui.base;

import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.help.GlideHelper;

import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Create by Mango at 19/9/18
 */
public class AdapterBinding {




    @BindingAdapter(value = {"imageUrl"}, requireAll = false)
    public static void loadUrl(ImageView view, String url) {
        if (!TextUtils.isEmpty(url)) GlideHelper.INSTANCE.loadImage(view,url);
    }

    @BindingAdapter(value = {"imageFileUrl"}, requireAll = false)
    public static void loadFileUrl(ImageView view, String url) {
        if (!TextUtils.isEmpty(url)) GlideHelper.INSTANCE.loadFileImage(view,url);
    }

    @BindingAdapter(value = {"visible"}, requireAll = false)
    public static void visible(View view, boolean visible) {
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    @BindingAdapter(value = {"showDrawable", "drawableShowed"}, requireAll = false)
    public static void showDrawable(ImageView view, boolean showDrawable, int drawableShowed) {
        view.setImageResource(showDrawable ? drawableShowed : R.color.transparent);
    }

    @BindingAdapter(value = {"textColor"}, requireAll = false)
    public static void setTextColor(TextView textView, int textColorRes) {
        textView.setTextColor(textView.getResources().getColor(textColorRes));
    }

    @BindingAdapter(value = {"imageRes"}, requireAll = false)
    public static void setImageRes(ImageView imageView, int imageRes) {
        imageView.setImageResource(imageRes);
    }

    @BindingAdapter(value = {"selected"}, requireAll = false)
    public static void selected(View view, boolean select) {
        view.setSelected(select);
    }

    @BindingAdapter(value = {"surfaceTexture"}, requireAll = false)
    public static void setSurfaceTexture(TextureView view, TextureView.SurfaceTextureListener textureListener) {
        view.setSurfaceTextureListener(textureListener);
    }

    @BindingAdapter(value = {"adapter","layoutManager","itemClickListener","loadMoreListener"},requireAll = false)
    public static void setAdapter(RecyclerView recyclerView, BaseQuickAdapter mAdapter, RecyclerView.LayoutManager layoutParams, OnItemClickListener itemClickListener, OnLoadMoreListener loadMoreListener) {
        recyclerView.setLayoutManager(layoutParams);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEmptyView(R.layout.recycle_empty);
//        mAdapter.setUseEmpty(false);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener(itemClickListener);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(loadMoreListener);
    }

    @BindingAdapter(value = {"bannerAdapter"}, requireAll = false)
    public static void setBannerAdapter(BGABanner view, BGABanner.Adapter<ImageView, String> bannerAdapter) {
        view.setAdapter(bannerAdapter);
    }
    @BindingAdapter(value = {"bannerFeedList","bannerTitleList"}, requireAll = false)
    public static void setBannerData(BGABanner view, List<String> bannerFeedList,List<String> bannerTitleList) {
        view.setAutoPlayAble(bannerFeedList.size()>1);
        view.setData(bannerFeedList,bannerTitleList);
    }
}
